package com.thinksns.sociax.t4.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewStub;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.thinksns.sociax.android.R;
import com.thinksns.sociax.t4.android.widget.roundimageview.RoundedImageView;
import com.thinksns.sociax.t4.component.GlideCircleTransform;
import com.thinksns.sociax.t4.model.FindListviewModel;
import com.thinksns.sociax.thinksnsbase.activity.widget.GlideRoundTransform;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/10/20.
 */

public class AdapterFindListview extends BaseAdapter {
    private Context context;
    private List<FindListviewModel> data;
    private int itemHerght;
    private LinearLayout.LayoutParams itemParams;
    private int mBottomMaxLine; // 设置底部 TextView 的最大行数

    private boolean isCircleImage;

    public AdapterFindListview(Context context, List<FindListviewModel> data, boolean isCircleImage, int bottomMaxLine, LinearLayout.LayoutParams itemParams) {
        this.context = context;
        if (data == null) {
            data = new ArrayList<>();
        }
        this.data = data;
        this.isCircleImage = isCircleImage;
        this.itemParams = itemParams;
        this.mBottomMaxLine = bottomMaxLine;
    }

    @Override
    public int getCount() {
        if (data == null) {
            return 0;
        }
        return data.size();
    }

    public void refresh(List<FindListviewModel> data) {
        this.data.clear();
        if (data != null) {
            this.data.addAll(data);
        }
        notifyDataSetChanged();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHoder hoder = null;
        if (convertView == null) {
            hoder = new ViewHoder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_find_listview, parent, false);
            hoder.image = (RoundedImageView) convertView.findViewById(R.id.img_head);
            hoder.image_meng = (ImageView) convertView.findViewById(R.id.img_head_back);
            hoder.tv_center = (TextView) convertView.findViewById(R.id.tv_center);
            hoder.tv_bottom = (TextView) convertView.findViewById(R.id.tv_bottom);
            hoder.rl_image = (RelativeLayout) convertView.findViewById(R.id.rl_image);
            hoder.rl_image.setLayoutParams(itemParams);
            /*接口返回认证图标，app端显示 8.7.2017*/
            hoder.stub_user_group = (ViewStub) convertView.findViewById(R.id.stub_user_group);
            if (itemParams != null){
                int user_group = itemParams.width * 6 / 20;
                RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) hoder.stub_user_group.getLayoutParams();
                if (params != null) {
                    params.width = user_group;
                    params.height = user_group;
                    hoder.stub_user_group.setLayoutParams(params);
                }
            }
            convertView.setTag(hoder);
        } else {
            hoder = (ViewHoder) convertView.getTag();
        }


        if (data.get(position).getType() == 0) {
            hoder.tv_center.setVisibility(View.VISIBLE);
            hoder.tv_bottom.setVisibility(View.GONE);
            hoder.image_meng.setVisibility(View.VISIBLE);
        } else {
            hoder.tv_center.setVisibility(View.GONE);
            hoder.tv_bottom.setVisibility(View.VISIBLE);
            hoder.image_meng.setVisibility(View.GONE);
            // 底部  hoder.tv_bottom 可见时，设置他的 maxLine
            if (mBottomMaxLine == 1) {
                hoder.tv_bottom.setSingleLine(true);
                hoder.tv_bottom.setMaxLines(1);
                hoder.tv_bottom.setTextColor(ContextCompat.getColor(context, R.color.trans_fant_color));
            } else if (mBottomMaxLine == 2) {
                hoder.tv_bottom.setSingleLine(false);
                hoder.tv_bottom.setMaxLines(2);
                hoder.tv_bottom.setTextColor(ContextCompat.getColor(context, R.color.smallFont));
            }
        }
//        Glide.with(context).load(R.drawable.find_item_huati_backgroud)
//                .diskCacheStrategy(DiskCacheStrategy.ALL).placeholder(R.drawable.default_user)
//                .transform(new GlideCircleTransform(context))
//                .crossFade()
//                .into(hoder.image);
        hoder.tv_center.setText(data.get(position).getTv_center());
        hoder.tv_bottom.setText(data.get(position).getTv_bottom());
        if (isCircleImage) {
            Glide.with(context).load(data.get(position).getImgUrl())
                    .diskCacheStrategy(DiskCacheStrategy.ALL).placeholder(R.drawable.default_user)
                    .transform(new GlideCircleTransform(context))
                    .crossFade()
                    .into(hoder.image);
             /*接口返回认证图标，app端显示 8.7.2017*/
            if (!TextUtils.isEmpty(data.get(position).getUser_group())){
//                DynamicInflateForWeibo.addUserGroup(context, hoder.stub_user_group, data.get(position).getUser_group());
            }else{
                hoder.stub_user_group.setVisibility(View.GONE);
            }
        } else {
            Glide.with(context).load(data.get(position).getImgUrl())
                    .diskCacheStrategy(DiskCacheStrategy.ALL).placeholder(R.drawable.image227x227)
                    .transform(new GlideRoundTransform(context, 10))
                    .centerCrop()
                    .dontAnimate()
                    .into(hoder.image);
        }
        return convertView;
    }

    class ViewHoder {

        ImageView image, image_meng;

        TextView tv_center;
        TextView tv_bottom;
        RelativeLayout rl_image;
        ViewStub stub_user_group;

    }
}
